/*	Copyright 2007 - Xavier Baro (xbaro@cvc.uab.cat)

	This file is part of eapmlib.

    Eapmlib is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or any 
	later version.

    Eapmlib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "IntImageData.h"

Evolutive::CIntImageData::CIntImageData(void) : CData(),m_ROI(NULL),m_Scale(1.0)
{	
	//! Define the data name
	m_DataName="INTEGRAL IMAGE";

#ifndef USE_OPENCV
	OPENCV_ERROR_CLASS("CIntImageData");
#endif
}

Evolutive::CIntImageData::~CIntImageData(void)
{
#ifdef USE_OPENCV
	register int i;
	if(m_DataPtr)
	{
		for(i=0;i<m_NumParameters;i++)
		{
			cvReleaseImageHeader((IplImage**)&(m_DataPtr[i]));
		}	
	}
	if(m_ROI)
		delete[] m_ROI;
#endif
}

#ifdef USE_OPENCV
Evolutive::CIntImageData::CIntImageData(IplImage *IntImage) : CData(),m_ROI(NULL),m_Scale(1.0)
{
	//! Define the data name
	m_DataName="INTEGRAL IMAGE";

	//! Set the number of parameters
	m_NumParameters=1;

	//! Create the pointers to the data structure
	m_DataPtr=new void*[m_NumParameters];

	//! Assign the pointer
	m_DataPtr[0]=(void*)IntImage;	
}

Evolutive::CIntImageData::CIntImageData(IplImage *IntImage,IplImage *RIntImage) : CData(),m_ROI(NULL),m_Scale(1.0)
{
	//! Define the data name
	m_DataName="INTEGRAL IMAGE";

	//! Set the number of parameters
	m_NumParameters=2;

	//! Create the pointers to the data structure
	m_DataPtr=new void*[m_NumParameters];

	//! Assign the pointer
	m_DataPtr[0]=(void*)IntImage;
	m_DataPtr[1]=(void*)RIntImage;
}

IplImage *Evolutive::CIntImageData::GetIntImage(void)
{
	if(m_NumParameters>0)
		return (IplImage*)m_DataPtr[0];
	else
		return NULL;
}

IplImage *Evolutive::CIntImageData::GetRotatedIntImage(void)
{
	if(m_NumParameters>1)
		return (IplImage*)m_DataPtr[1];
	else
		return NULL;
}

void Evolutive::CIntImageData::SetROI(CvRect ROI)
{
	if(!m_ROI)
		m_ROI=new CvRect[1];
	*m_ROI=ROI;
}

void Evolutive::CIntImageData::ResetROI(void)
{
	if(m_ROI)
		delete[] m_ROI;
	m_ROI=NULL;
}

int Evolutive::CIntImageData::GetOffset(void)
{
	if(!m_DataPtr)
		return 0;

	if(!m_ROI)
		return 0;

	// Obtain the width step (aligned data)
	int WidthStep=((IplImage*)m_DataPtr[0])->widthStep;

	// Convert the offset to pixels, not to bytes
	WidthStep/=4.0;

	return (m_ROI->y*WidthStep+m_ROI->x);
}

void Evolutive::CIntImageData::SetScale(double Scale)
{
	m_Scale=Scale;
}


double Evolutive::CIntImageData::GetScale(void)
{
	return m_Scale;
}

CvRect* Evolutive::CIntImageData::GetROI(void)
{
	return m_ROI;
}
#endif //USE_OPENCV	
